package org.web.ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.web.ssm.dto.ResultModel;
import org.web.ssm.model.Product;
import org.web.ssm.model.Topic;
import org.web.ssm.model.queryVo.BaseQuery;
import org.web.ssm.service.IProductService;
import org.web.ssm.service.ITopicService;
import org.web.ssm.utils.DateUtils;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created by hubin on 2016/6/15.
 */
@Controller
@RequestMapping("topic")
public class TopicController {

    @Autowired
    private ITopicService topicService;

    @RequestMapping(value = "/addtopic", method = RequestMethod.POST)
    @ResponseBody
    public ResultModel<Topic> addTopic(String title, String createTime, String content) {
        ResultModel<Topic> resultModel = new ResultModel<Topic>();
        Topic topic = new Topic();
        try {
            topic.setContent(content);
            topic.setCreateTime(DateUtils.stringToDate(createTime));
            topic.setTitle(title);
            topicService.addTopic(topic);
            resultModel.setMsg("添加成功！");
            resultModel.setSuccess(true);
            resultModel.setModel(topic);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel.setMsg("添加失败！");
            resultModel.setSuccess(false);
        }
        return resultModel;
    }

    @RequestMapping("/updatetopic")
    @ResponseBody
    public ResultModel<Topic> updateTopic(String title, String content, int id) {
        ResultModel<Topic> resultModel = new ResultModel<Topic>();
        Topic topic = new Topic();
        try {
            topic.setContent(content);
            topic.setTitle(title);
            topic.setId(id);
            topicService.updateTopic(topic);
            resultModel.setMsg("添加成功！");
            resultModel.setSuccess(true);
            resultModel.setModel(topic);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel.setMsg("添加失败！");
            resultModel.setSuccess(false);
        }
        return resultModel;
    }

    @RequestMapping("/deltopic")
    @ResponseBody
    public ResultModel<Topic> delTopic(int id) {
        ResultModel<Topic> result = new ResultModel<Topic>();
        try {
            topicService.delTopic(id);
            result.setMsg("删除成功！");
            result.setSuccess(true);

        } catch (Exception e) {
            result.setMsg("删除失败！");
            result.setSuccess(false);
        }
        return result;
    }

    @RequestMapping(value = "/gettopiclist", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultModel<BaseQuery<Topic>> getProductList(HttpSession httpSession, int pageSize, int pageCurrent, String keyword) {
        ResultModel<BaseQuery<Topic>> product = new ResultModel<BaseQuery<Topic>>();
        try {
            BaseQuery<Topic> productQuery = topicService.getTopicList(1, pageSize, pageCurrent, keyword);
            product.setModel(productQuery);
            product.setSuccess(true);
            product.setMsg("");
        } catch (Exception e) {

        }
        return product;
    }
}
